package com.zj.study.controller;

import com.zj.study.config.WebConfiguration;
import com.zj.study.entity.ADTO;
import com.zj.study.entity.StationBulletinDTO;
import com.zj.study.entity.User;
import com.zj.study.pojo.FinalResultVO;
import com.zj.study.service.UserService;
import com.zj.study.subscribere.RedisSubscriber;
import com.zj.study.utils.util.IPUtils;
import com.zj.study.utils.util.WebUtils;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
//import reactor.core.publisher.Mono;

/**
 * @author 赵赳
 * @since: 2021/8/6 9:17
 */
@RestController
@Slf4j
public class TestController {

  private final StringRedisTemplate redisTemplate;


  private final ThreadPoolExecutor executor;

  private UserService userService;

  @Autowired
  public TestController(StringRedisTemplate redisTemplate, ThreadPoolExecutor executor) {
    this.redisTemplate = redisTemplate;
    this.executor = executor;
  }

  @Autowired
  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  /**
   * test
   */
  @PostMapping("/testJson")
  public FinalResultVO<ADTO> testJson(@RequestBody ADTO adto) {
    return FinalResultVO.ok(adto);
  }



  /**
   * 测试获取id
   *
   * @return id
   */
  @PostMapping("/getId")
  public FinalResultVO<Long> getId() {
    var user = new User().setName("aa").setAge(10);
    userService.save(user);
    var id = user.getId();
    return FinalResultVO.ok(id);
  }


  /**
    * 测试excel
   */
  @PostMapping("/test/excel")
  public FinalResultVO<Void> testExcel() throws FileNotFoundException {
    userService.testExcel();
    return FinalResultVO.ok();
  }


  /**
   * 自定义分页测试(物理分页)
   *
   * @return 分页数据
   */
  @PostMapping("/page")
  public FinalResultVO<List<User>> getPage(@RequestParam Integer pageNum,
      @RequestParam Integer pageSize) {
    return userService.getPage(pageNum, pageSize);
  }

  /**
   * test demo
   *
   * @param id id
   * @return id
   */
  @GetMapping("/testGet")
  public String testGet(@RequestParam String id) {
    return id;
  }

  /**
   * 测试线程池
   */
  @GetMapping("/testExecutor")
  public String testExecutor() {
    CompletableFuture.runAsync(() -> log.info("创建异步线程池"), executor);
    log.info("当前线程池；{}", Thread.currentThread().getName());
    return "success";
  }


  /**
   * 测试获取请求ip地址
   *
   * @param request http请求
   * @return ip
   */
  @GetMapping("getIPAdder")
  public FinalResultVO<String> getIPAddress(ServerHttpRequest request) {
    var ipAddr = IPUtils.getIpAddress(request);
    return FinalResultVO.ok(ipAddr, "请求ip地址");
  }


  ///**
  // * 获取threadLocal数据
  // *
  // * @return 用户数据
  // */
  //@GetMapping("getLocal")
  //public Mono<FinalResultVO<List<User>>> getLocal() {
  //  return Mono.fromSupplier(() -> FinalResultVO.ok(WebConfiguration.LOCAL.get("user")));
  //}

  /**
   * 测试SpringMvc 获取Map参数数据
   *
   * @param params map数据
   * @return 响应
   */
  @PostMapping("/map")
  public String getMapParams(@RequestBody Map<String, Object> params) {
    //接收json数据
    System.out.println("params = " + params);
    var address = params.get("address");
    System.out.println("address = " + address);
    return null;
  }


  @PostMapping("/upload")
  public String upload(MultipartFile file) {
    return null;
  }

  /**
   * 测试HttpClient
   *
   * @return
   */
  @GetMapping("/hello")
  public String testClient() {
    var clientIp = WebUtils.getClientIp();
    System.out.println("clientIp = " + clientIp);
    return clientIp;
  }

  /**
   * 测试redis的订阅模式
   *
   * @return 响应
   * @see RedisSubscriber#onMessage
   */
  @GetMapping("/test/{orderNo}")
  public String testRedis(@PathVariable("orderNo") String orderNo) {
    redisTemplate.opsForValue().set("expired_order:" + orderNo, "", 5, TimeUnit.SECONDS);
    log.info("成功发送消息！");
    return "success";
  }

  /**
   * @param dto dto
   */
  @PostMapping("/aaa")
  public void testDate(StationBulletinDTO dto) {
    System.out.println("dto = " + dto);
  }


}
